package com.xneure.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StrUtil{
	/**
	 * 生成字符串校验码
	 * 
	 * @param str
	 * @return String
	 */
	public static char getVerifyCode(String str){
		return (char)((getStringMark(str) / 16) + 65);
	}

	/**
	 * 字符串生产唯一标识
	 * 
	 * @param str
	 * @return int
	 */
	public static int getStringMark(String str){
		int l = 0;
		for(char c:str.toCharArray()){
			l = l ^ c;
		}
		return l;
	}

	public static int getStringLength(String str){
		int length = 0;
		for(char c:str.toCharArray()){
			if(((c & 0xFF00) >> 8) == 0){
				length++;
			}else{
				length += 2;
			}
		}
		return length;
	}

	/**
	 * 计算字符串的行数,忽略换行符。
	 * 
	 * @param data 要计算的数据
	 * @param rowCount 每行显示的字符串
	 * @return int
	 */
	public static int stringRowCount(String data,int rowCount){
		char[] tb = data.toCharArray();
		rowCount *= 2;
		int l = 0, len = 1;
		boolean en = false;
		for(int i = 0;i < tb.length;i++){
			if(tb[i] == 13 || tb[i] == 10){
				continue;
			}
			if(((tb[i] & 0xFF00) >> 8) == 0){
				if(en == false){
					l--;
				}else{
					l++;
				}
				en = !en;
			}
			l++;
			if(l == rowCount){
				len++;
				l = 0;
			}
		}
		return len;
	}

	/**
	 * 拼接字符串
	 * 
	 * @param str
	 * @return String
	 */
	public static String splicingString(String... str){
		if(ObjectUtil.isNull(str)){
			return null;
		}
		int len = 0;
		for(String s:str){
			if(s != null){
				len += s.length();
			}
		}
		StringBuilder sBuilder = new StringBuilder(len);
		for(String s:str){
			sBuilder.append(s);
		}
		return sBuilder.toString();
	}

	public static char[] delNullChar(char[] cs){
		char[] tmpCh = new char[cs.length];
		int i = 0;
		for(char c:cs){
			if(c != '\0'){
				tmpCh[i++] = c;
			}
		}
		return Arrays.copyOfRange(tmpCh,0,i);
	}

	/**
	 * 字符串转数组
	 * 
	 * @param str
	 * @param mak
	 * @return String[]
	 */
	public static String[] strToArray(String str,String mak){
		if(str.indexOf(mak) > 0){
			return str.split(mak);
		}else{
			return new String[]{str};
		}
	}

	public static String[] strToArray(String str){
		char[] cs = str.toCharArray();
		int length = cs.length;
		List<String> strList = new ArrayList<String>(length > 100 ? 20 : length / 3);
		int startIndex = 0;
		int endIndex = 0;
		for(int i = 0;i < length;i++){
			if(cs[i] == '\r'){
				endIndex = i - isLineMake(cs,i);
				if(endIndex - startIndex > 0){
					strList.add(new String(Arrays.copyOfRange(cs,startIndex,endIndex)));
				}else{
					strList.add("");
				}
				startIndex = i + 1;
				if(cs[i + 1] == '\n'){
					++startIndex;
				}
			}
		}
		if(startIndex < length){
			strList.add(new String(Arrays.copyOfRange(cs,startIndex,length - isLineMake(cs,length) - isLineMake(cs,length - 2))));
		}
		return strList.toArray(new String[strList.size()]);
	}

	public static int isLineMake(char[] cs,int index){
		return cs[index - 1] == '\n' || cs[index - 1] == '\r' ? 1 : 0;
	}

	public static String join(String[] str,String s){
		if(str.length < 2){
			return str[0];
		}
		StringBuilder sb = new StringBuilder();
		for(String t:str){
			sb.append(s).append(t);
		}
		return sb.substring(s.length());
	}

	public static String fillString(CharSequence cs,int leng){
		StringBuilder sb = new StringBuilder(leng);
		for(int i = 0;i < leng;i++){
			sb.append(cs);
		}
		return sb.toString();
	}

	public static String emojiToImg(String str){
		char[] chs = str.toCharArray();
		StringBuilder sb = new StringBuilder(chs.length);
		for(char c:chs){
			if((isChinese(c) == false && isEnglishChar(c) == false && isSign(c) == false && isNum(c) == false) || isEmoji(c)){
				sb.append("[").append((int)c).append("]");
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static boolean isEmoji(char c){
		return c >= 57416 && c <= 58402;
	}

	public static boolean isEnglishChar(char c){
		return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == ' ';
	}

	public static boolean isSign(char c){
		return c >= ' ' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~';
	}

	public static boolean isNum(char c){
		return c >= '0' && c <= '*' || c == '.';
	}

	// 根据Unicode编码完美的判断中文汉字和符号
	public static boolean isChinese(char c){
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION)
		{
			return true;
		}
		return false;
	}

	// 完整的判断中文汉字和符号
	public static boolean isChinese(String strName){
		char[] ch = strName.toCharArray();
		for(int i = 0;i < ch.length;i++){
			char c = ch[i];
			if(isChinese(c)){
				return true;
			}
		}
		return false;
	}

	public static InputStream strToStream(String str){
		return new ByteArrayInputStream(str.getBytes());
	}

	public static String streamToStr(InputStream is){
		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();
		try{
			String line = "";
			while((line = in.readLine()) != null){
				buffer.append(line).append("\r\n");
			}
		}catch(IOException e){
		}
		return buffer.toString();
	}

	public static int strfind(String fStr,String str){
		if(fStr == null || str == null){
			return -1;
		}
		char[] fChars = fStr.toCharArray();
		char[] chars = str.toCharArray();
		int fLength = fStr.length() - 1;
		if(str.length() < fStr.length()){
			return -1;
		}else{
			if(fStr.length() == str.length()){
				if(fChars[0] == chars[0]){
					if(fChars[fLength] == chars[fLength]){
						if(Arrays.equals(fChars,chars)){
							return 0;
						}
					}
				}
				return -1;
			}
		}
		int sIndex = -1;
		int eIndex = -1;
		int strLength = chars.length - 1;
		int endPoint = strLength - fChars.length;
		for(int i = 0;i < endPoint;i++){
			if(fChars[0] == chars[i]){
				sIndex = i;
				break;
			}
		}
		if(sIndex < 0 || sIndex > endPoint){
			return -1;
		}
		for(;;){
			if(fChars[fLength] == chars[sIndex + fLength]){
				if(fChars[1] == chars[sIndex + 1]){
					boolean isEnd = false;
					for(int i = 2;i < fLength;i++){
						if(fChars[i] != chars[sIndex + i]){
							isEnd = true;
							break;
						}
					}
					if(isEnd == false){
						return sIndex;
					}
				}
			}
			for(int i = sIndex + fLength;i < endPoint;i++){
				if(fChars[0] == chars[i]){
					sIndex = i;
					break;
				}
			}
			for(int i = sIndex + fLength;i <= strLength;i++){
				if(fChars[fLength] == chars[i]){
					eIndex = i;
					if(i - fLength > sIndex){
						sIndex = i - fLength;
					}
					break;
				}
			}
			if(eIndex > strLength){
				return -1;
			}
		}
	}
}
